package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.enm.AttentionFrom;
import com.umakr.ax.common.mapper.ShowDiscussMapper;
import com.umakr.ax.common.model.RadioStationShow;
import com.umakr.ax.common.model.ShowDiscuss;
import com.umakr.ax.common.model.ShowFabulous;
import com.umakr.ax.common.vo.ShowDiscussVO;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.FormatPhotoUrlUtil;
import com.umakr.ax.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 秀评论 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class ShowDiscussService extends BaseService<ShowDiscussMapper, ShowDiscuss>{
    @Autowired
    RadioStationShowService radioStationShowService;
    @Autowired
    ShowDiscussService showDiscussServices;
    @Autowired
    ShowFabulousService showFabulousService;
    @Autowired
    RadioStationFollowerService radioStationFollowerService;
    @Autowired
    Environment env;

    private static final String SHOW_ID = "show_id";
    private static final String DEL_STATUS = "del_status";

    public Object discussDel(Long userId, Long rsId, Long showId, Long id) {
        int errorCode = 0 ;
        String msg = "删除失败";
        Map<String,Object> data = Maps.newLinkedHashMap();
        if(id != null){
            ShowDiscuss showDiscuss = selectById(id);
            if (showDiscuss != null){
                //此处留出位置判断是否只有用户自己或者电台才可以删除评论

                showDiscuss.setDelStatus(1);
                update(showDiscuss);
                data.put("result",1);
                msg = "删除成功";
                errorCode = 1;
                //删除评论成功后需要更新秀的评论数据
                RadioStationShow radioStationShow =  radioStationShowService.queryById(showId);
                if(radioStationShow != null){
                    EntityWrapper<ShowDiscuss> showDiscussWrapper = new EntityWrapper<>();
                    showDiscussWrapper.eq(SHOW_ID,showId);
                    showDiscussWrapper.eq(DEL_STATUS,0);
                    //如果是审核后才能评论的话，需要新增一个筛选条件
                    if(radioStationShow.getShowReviewPermissions() == 1){
                        showDiscussWrapper.eq("review_status",1);
                    }
                    Integer showDiscussCount = selectCount(showDiscussWrapper);
                    radioStationShow.setCommentCount(showDiscussCount);
                    radioStationShowService.updateRadioStationShow(radioStationShow);
                }
            }
        }
        return  resultMap(data,errorCode,msg);
    }

    public Object discuss(Long userId, Long rsId, Long showId, String commentContent) {
        int errorCode = 0 ;
        String msg ;
        Map<String,Object> data = Maps.newLinkedHashMap();
        Date date = new Date();
        RadioStationShow radioStationShow = radioStationShowService.queryById(showId);
        if(radioStationShow != null){
            Integer permissions = radioStationShow.getShowReviewPermissions();
            ShowDiscuss showDiscuss = new ShowDiscuss();
            showDiscuss.setUserId(userId);
            showDiscuss.setRsId(rsId);
            showDiscuss.setShowId(showId);
            showDiscuss.setCommentContent(commentContent);
            showDiscuss.setCommentTime(date);
            showDiscuss.setDelStatus(0);
            //根据Permission的值：0 禁止评论 1 审核后评论 2 直接评论
            //如果是直接评论的话，审核状态设置为1 否则设置状态为 0
            int permissionsType = 2;
            if(permissions == permissionsType){
                showDiscuss.setReviewStatus(1);
            }else {
                showDiscuss.setReviewStatus(0);
            }

            ShowDiscuss records = update(showDiscuss);
            data.put("id",records.getId());
            data.put("userId",records.getUserId());
            data.put("rsId",records.getRsId());
            data.put("showId",records.getShowId());
            data.put("commentContent",records.getCommentContent());
            data.put("commentTime",records.getCommentTime());
            data.put("reviewStatus",records.getReviewStatus());
            radioStationFollowerService.autoAttention(userId,rsId, AttentionFrom.FAVORITE);
            msg = "评论成功";
            errorCode = 1;

            //评论成功后需要更新秀的评论数据
            EntityWrapper<ShowDiscuss> showDiscussWrapper = new EntityWrapper<>();
            showDiscussWrapper.eq(SHOW_ID,showId);
            showDiscussWrapper.eq(DEL_STATUS,0);
            Integer showDiscussCount = selectCount(showDiscussWrapper);

            radioStationShow = radioStationShowService.queryById(showId);
            //只有无需审核直接评论的电台秀才统计评论数量
            if(radioStationShow != null && radioStationShow.getShowReviewPermissions() == permissionsType){
                radioStationShow.setCommentCount(showDiscussCount);
                radioStationShowService.updateRadioStationShow(radioStationShow);
            }
        }else {
            msg = "您当前评论的电台秀已经删除!";
        }
        return  resultMap(data,errorCode,msg);
    }

    public Object load(Long userId, Long showId) {
        String aliUrl = env.getProperty("oss.img.path");
        Page<ShowDiscussVO> page = baseMapper.load(userId,showId);
        //修改头像链接地址
        for (ShowDiscussVO showDiscussVO : page.getRecords()){
            showDiscussVO.setHeadImgUrl(FormatPhotoUrlUtil.formatString(aliUrl,showDiscussVO.getHeadImgUrl()));
        }
        //获取评论数量
        EntityWrapper<ShowDiscuss> showDiscussWrapper = new EntityWrapper<>();
        showDiscussWrapper.eq(SHOW_ID,showId);
        showDiscussWrapper.eq(DEL_STATUS,0);
        showDiscussWrapper.eq("review_status",1);
        Integer showDiscussCount = showDiscussServices.selectCount(showDiscussWrapper);

        //获取点赞数量
        EntityWrapper<ShowFabulous> showFabulousWrapper = new EntityWrapper<>();
        showFabulousWrapper.eq(SHOW_ID,showId);
        Integer fabulousCount = showFabulousService.selectCount(showFabulousWrapper);

        WebUtil.addParamToPage(page,"commentCount",showDiscussCount);
        WebUtil.addParamToPage(page,"likeCount",fabulousCount);
        return page;
    }

    public List<ShowDiscussVO> selectTopShowDiscussByShowId(Long showId,Long userId) {
        return baseMapper.selectTopShowDiscussByShowId(showId,userId);
    }

}
